package com.click369.mianshiqingbao.service.impl;

import com.click369.mianshiqingbao.mapper.CommentBeanMapper;
import com.click369.mianshiqingbao.pojo.CommentBean;
import com.click369.mianshiqingbao.pojo.CommentBeanExample;
import com.click369.mianshiqingbao.pojo.CommentBeanExample.Criteria;
import com.click369.mianshiqingbao.pojo.entity.PageResult;
import com.click369.mianshiqingbao.service.CommentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 评论信息的业务访问接口实现类
 */
@Service
public class CommentServiceImpl implements CommentService{
    @Autowired
    private CommentBeanMapper commentBeanMapper;
    /**
     * 条件分页查询评论信息列表
     * @return 分页查询信息的封装类对象
     */
    @Override
    public PageResult findPage(CommentBean commentbean, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<CommentBean> page=(Page<CommentBean>)commentBeanMapper.selectByExample(null);
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 增加评论信息
     * @param commentbean 评论信息对象
     */
    @Override
    public void add(CommentBean commentbean) {
        commentBeanMapper.insert(commentbean);
    }
    /**
     * 根据ID获取评论信息对象
     * @param id 评论信息的id
     * @return 评论信息对象
     */
    @Override
    public CommentBean findOne(Long id) {
        return commentBeanMapper.selectByPrimaryKey(id);
    }
    /**
     * 修改评论信息
     * @param commentbean 评论信息对象
     */
    @Override
    public void update(CommentBean commentbean) {
        commentBeanMapper.updateByPrimaryKey(commentbean);
    }
    /**
     * 批量删除评论信息
     * @param ids 批量删除评论信息的id集合
     */
    @Override
    public void delete(Long[] ids) {
        for(long id:ids){
            commentBeanMapper.deleteByPrimaryKey(id);
        }
    }
    /**
     * 根据当前题目Id获取评论列表
     * @param topicId 题目Id
     * @return 评论列表
     */
    @Override
    public List<CommentBean> getCommentBeanList(Long topicId) {
        CommentBeanExample example = new CommentBeanExample();
        Criteria criteria = example.createCriteria();
        criteria.andTopicIdEqualTo(topicId);
        return commentBeanMapper.selectByExample(example);
    }

}
